﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PersonalAccounting.Business;

namespace PersonalAccounting.Forms
{
    public partial class FormConsTotalesPorMes : Form
    {

        #region Form

        public FormConsTotalesPorMes()
        {
            InitializeComponent();
            cboCuenta.CargarCuentas();
        }

        private void FormConsTotalesPorMes_Load(object sender, EventArgs e)
        {
            dtpDesde.Value = new DateTime(DateTime.Today.Year, 1, 1);
            dtpHasta.Value = DateTime.Today;
        }

        #endregion


        #region DoubleClick

        private void lvDatos_DoubleClick(object sender, EventArgs e)
        {
            if (lvDatos.SelectedItems.Count > 0)
            {
                ListViewItem item = lvDatos.SelectedItems[0];
                DateTime fecha = (DateTime)item.Tag;
                FormConsEstadoDeCuenta form = new FormConsEstadoDeCuenta();

                form.MdiParent = this.MdiParent;
                form.Show();

                form.Desde = fecha;
                form.Hasta = fecha.AddMonths(1).AddDays(-1);
                form.CuentaSeleccionada = cboCuenta.Value;

                form.btnConsultar_Click(sender, e);
            }
        }

        #endregion

        #region Click

        private void btnConsultar_Click(object sender, EventArgs e)
        {
            if (CondicionesBuscar())
                Buscar();
        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        #endregion


        #region Acciones

        private void Buscar()
        {
            QTotalsPerMonth query = new QTotalsPerMonth(dtpDesde.Value, 
                                                        dtpHasta.Value, 
                                                        cboCuenta.Value);
            String format = "###,###,###.00";

            lvDatos.Items.Clear();

            foreach (RegMonthTotal reg in query.Result)
            {
                ListViewItem item = lvDatos.Items.Add(FormatMonthYear(reg.Year, reg.Month));

                item.SubItems.Add(reg.TotalIn.ToString(format));
                item.SubItems.Add(reg.TotalOut.ToString(format));
                item.SubItems.Add((reg.TotalIn - reg.TotalOut).ToString(format));
                item.Tag = new DateTime(reg.Year, reg.Month, 1);

                if (item.Index % 2 != 0)
                    item.BackColor = Color.WhiteSmoke;

                if (reg.TotalOut > reg.TotalIn)
                    item.ForeColor = Color.Red;
            }
        }

        private string FormatMonthYear(int year, int month)
        {
            DateTime date = new DateTime(year, month, 1);

            return Capitalize(date.ToString("MMMM yyyy"));
        }

        private string Capitalize(string text)
        {
            return text.Substring(0, 1).ToUpper() + text.Substring(1);
        }

        #endregion

        #region Condiciones

        private bool CondicionesBuscar()
        {
            if (dtpHasta.Value < dtpDesde.Value)
            {
                MessageBox.Show("Las fechas indicadas no son correctas.",
                    this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                dtpDesde.Focus();
                return false;
            }

            return true;
        }

        #endregion

    }
}